rm(list=ls())

#function to install packages if they don't exist
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("foreign", "readstata13","car","psych","psy","stargazer","dplyr","stringr","lavaan")
ipak(packages)

#start zero function - needed to run the models below
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}


#load datasets
wvs<-read.dta13("Study3_wvs_subset.dta") #ignore warnings - all about labels
politics2012<-read.dta13("Study3_politics_12_subset.dta") #ignore warnings - all about labels
personality2011<-read.dta13("Study3_personality_2011_subset.dta") #ignore warnings - all about labels
personality2012<-read.dta13("Study3_personality_2012_subset.dta") #ignore warnings - all about labels
background2012<-read.dta13("Study3_background_2012_subset.dta") #ignore warnings - all about labels

#merge waves into one dataset
wvs_1 <- merge(wvs, personality2011,by="nomem_encr", all=T)
wvs_2 <- merge(wvs_1, personality2012, by="nomem_encr", all=T)
wvs_3 <- merge(wvs_2, politics2012, by="nomem_encr", all=T)
wvs_full <- merge(wvs_3, background2012, by="nomem_encr", all=T)

#save data
save(wvs_full, file="study3_raw_data.RData")

##################################
#
#Left-right ideology
#
##################################
#set don't know to missing
wvs_full$wvs_lrplacement <-car::recode(as.numeric(wvs_full$hz12a102),"999=NA", as.numeric=T)
#code to range from 0 to 1
wvs_full$wvs_lrplacement <- ((wvs_full$wvs_lrplacement-1)/9)

##################################
#
#Social ideology
#
##################################

#Purity 1: men and woman kiss in public
#(1-5 with 6 don't know): #strongly disagree - strongly agree
#code to range fro 0 - 1 & set don't know to missing
wvs_full$purity <-car::recode(as.numeric(wvs_full$hz12a055),"1=0; 2=0.25; 3=.5; 4=.75; 5=1; 6=NA", as.numeric=T)

#Gay 1:  men kiss in public
#(1-5 with 6 missing): strongly disagree - strongly agree
#set don't know to missing
wvs_full$gay1 <-car::recode(as.numeric(wvs_full$hz12a056),"1=0; 2=0.25; 3=.5; 4=.75; 5=1; 6=NA", as.numeric=T)

#Gay 2:
#Wording: Gay men and lesbian women should be free to live their life as they wish.
# (1-5 with 6 missing): strongly disagree - strongly agree
#set don't know to missing and recode to range from agree to disagree
wvs_full$gay2 <-car::recode(as.numeric(wvs_full$hz12a057), "1=5; 2=4; 4=2; 5=1; 6=NA", as.numeric=T)
#code to range from 0 - 1
wvs_full$gay2 <-car::recode(as.numeric(wvs_full$gay2),"1=0; 2=0.25; 3=.5; 4=.75; 5=1", as.numeric=T)

#abortion
#wording: For each of the following actions indicate whether you think it can always be justified, never be justified, or something in between? Abortion
#(1-10 with 11 don't know): never to always
#recode to range from always (lib) to never (cons)
wvs_full$abortion <-car::recode(as.numeric(wvs_full$hz12a224),"1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1; 11=NA", as.numeric=T)
#recode to range from 0-1
wvs_full$abortion <- ((wvs_full$abortion-1)/9)

#Euthanasia 
#wording: Where would you place yourself on a scale from 1 to 5, where 1 means that euthanasia should be forbidden and 5 means that euthanasia should be permitted?
#1 forbidden to permitted; 99 don't know
#recod to range from permitted to forbidden and set don't know to missing
wvs_full$euthanasia <-car::recode(as.numeric(wvs_full$cv13f102), "1=5; 2=4; 4=2; 5=1; 99=NA", as.numeric=T)
#recode to range from 0 - 1
wvs_full$euthanasia<- ((wvs_full$euthanasia-1)/4)

#Immigrants 
#THere are too many people of foreign descent
#fully disagree to fully agree
wvs_full$immigration<-((wvs_full$cv13f120-1)/4)

#Immigrants 
#retain own culture vs. adapt to dutch
#fully disagree to fully agree
wvs_full$immigration1 <-car::recode(as.numeric(wvs_full$cv13f104), "99=NA", as.numeric=T)
wvs_full$immigration1<-((wvs_full$immigration1-1)/4)

##################################
#
#Economic ideology
#
##################################
#support redistribution
wvs_full$redistribution1 <-car::recode(as.numeric(wvs_full$hz12a103),"11=NA", as.numeric=T)
#*recode to range from 0 (support redistriubiotn) - 1 (oppose redistribution) 
wvs_full$redistribution1 <- ((wvs_full$redistribution1-1)/9)

#*government should take care of everybodya
#set don't know to missing
wvs_full$govrespon <-car::recode(as.numeric(wvs_full$hz12a105),"11=NA", as.numeric=T)
#code to range from 0 - 1
wvs_full$govrespon <- ((wvs_full$govrespon-1)/9)

#income differences should increas vs. should decrease
#set don't know to missing
wvs_full$redistribution2 <-car::recode(as.numeric(wvs_full$cv13f103),"1=5; 2=4; 4=2; 5=1; 99=NA", as.numeric=T)
#code to range from 0 - 1
wvs_full$redistribution2 <- ((wvs_full$redistribution2-1)/4)

##########################
#generate scales
##########################

#social ideology
wvs_full$wvs_moral<-(rowMeans(data.frame(wvs_full$purity, wvs_full$gay1, wvs_full$gay2, wvs_full$abortion, wvs_full$euthanasia, wvs_full$immigration), na.rm=T))

#economic ideology
wvs_full$wvs_economicattitudes<-(rowMeans(data.frame(wvs_full$redistribution1, wvs_full$redistribution2, wvs_full$govrespon), na.rm=T))

##############################
#
#
#Big Five Personality Inventory (10 items)
# "I see myself as someone with an ....
#
#
##############################


#**********************
#*Openness
#**********************

#*(E) has an active imagination.
#set don't know to missing
wvs_full$open_item1 <-car::recode(as.numeric(wvs_full$hz12a178),"6=NA", as.numeric=T)

#* (J) has few artistic interests (R)
#set don't know to missing and recode to reversed scored item
wvs_full$open_item2 <-car::recode(as.numeric(wvs_full$hz12a173),"1=5; 2=4; 4=2; 5=1; 6=NA", as.numeric=T)

#r=.30
cor(data<-(data.frame(wvs_full$open_item1, wvs_full$open_item2)), use="complete.obs", method="pearson") 
#Cronbach's alpha = 0.46
data_open <- (data.frame(wvs_full$open_item1, wvs_full$open_item2))
cronbach(data_open)

#create Openness scale
wvs_full$BFI_open<-(rowMeans(data.frame(wvs_full$open_item1, wvs_full$open_item2), na.rm=T))

#**********************
#*Conscientiousness
#**********************

#*(C) does a thorough job 
#set don't know to missing
wvs_full$con_item1 <-car::recode(as.numeric(wvs_full$hz12a176),"6=NA", as.numeric=T)

#*(H) tends to be lazy. (R)
#set don't know to missing and recode to reversed scored item
wvs_full$con_item2 <-car::recode(as.numeric(wvs_full$hz12a171),"1=5; 2=4; 4=2; 5=1; 6=NA", as.numeric=T)

#r=.26
cor(data<-(data.frame(wvs_full$con_item1, wvs_full$con_item2)), use="complete.obs", method="pearson") 
#Cronbach's alpha = 0.42
data_con <- (data.frame(wvs_full$con_item1, wvs_full$con_item2))
cronbach(data_con)

#create Conscientiousness scale
wvs_full$BFI_con<-(rowMeans(data.frame(wvs_full$con_item1, wvs_full$con_item2), na.rm=T))

#**********************
#Extraversion 
#**********************

#*(F) is outgoing, sociable.
wvs_full$ext_item1 <-car::recode(as.numeric(wvs_full$hz12a174),"6=NA", as.numeric=T)

#(A) is reserved. (R)
#set don't know to missing and recode to reversed scored item
wvs_full$ext_item2 <-car::recode(as.numeric(wvs_full$hz12a169),"1=5; 2=4; 4=2; 5=1; 6=NA", as.numeric=T)

#r=.26
cor(data<-(data.frame(wvs_full$ext_item1, wvs_full$ext_item2)), use="complete.obs", method="pearson") 
#Cronbach's alpha = 0.41
data_ext <- (data.frame(wvs_full$ext_item1, wvs_full$ext_item2))
cronbach(data_ext)

#create Extraversion scale
wvs_full$BFI_ext<-(rowMeans(data.frame(wvs_full$ext_item1, wvs_full$ext_item2), na.rm=T))

#**********************
#*Agreeableness
#**********************

#*(B) Is generally trusting
wvs_full$agre_item1 <-car::recode(as.numeric(wvs_full$hz12a170),"6=NA", as.numeric=T)

#*(G) tends to fault others (R)
#set don't know to missing and recode to reversed scored item
wvs_full$agre_item2 <-car::recode(as.numeric(wvs_full$hz12a175),"1=5; 2=4; 4=2; 5=1; 6=NA", as.numeric=T)

#r=.11
cor(data<-(data.frame(wvs_full$agre_item1, wvs_full$agre_item2)), use="complete.obs", method="pearson") 
#Cronbach's alpha = 0.21
data_agre <- (data.frame(wvs_full$agre_item1, wvs_full$agre_item2))
cronbach(data_agre)

#create Agreeableness scale
wvs_full$BFI_agre<-(rowMeans(data.frame(wvs_full$agre_item1, wvs_full$agre_item2), na.rm=T))

#**********************
#*Neuroticism
#**********************

#*(I) gets nervous easily 
wvs_full$neu_item1 <-car::recode(as.numeric(wvs_full$hz12a177),"6=NA", as.numeric=T)

#*(D) is relaxed, handless stress well. (R)
#set don't know to missing and recode to reversed scored item
wvs_full$neu_item2 <-car::recode(as.numeric(wvs_full$hz12a172),"1=5; 2=4; 4=2; 5=1; 6=NA", as.numeric=T)

#r=.52
cor(data<-(data.frame(wvs_full$neu_item1, wvs_full$neu_item2)), use="complete.obs", method="pearson") 
#Cronbach's alpha = 0.68
data_neu <- (data.frame(wvs_full$neu_item1, wvs_full$neu_item2))
cronbach(data_neu)

#create Neuroticism scale
wvs_full$BFI_neu<-(rowMeans(data.frame(wvs_full$neu_item1, wvs_full$neu_item2), na.rm=T))

##############################
#
#
#IPIP FFM (50 items)
#
#
##############################

#Openness in 2011
wvs_full$y11_open1 <-wvs_full$cp11d024
wvs_full$y11_open2 <-wvs_full$cp11d034 
wvs_full$y11_open3 <-wvs_full$cp11d044
wvs_full$y11_open4 <-wvs_full$cp11d054
wvs_full$y11_open5 <-wvs_full$cp11d059
wvs_full$y11_open6 <-wvs_full$cp11d064
wvs_full$y11_open7 <-wvs_full$cp11d069
#reversed coded items
wvs_full$y11_rec_open8 <-car::recode(as.numeric(wvs_full$cp11d029),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_open9 <-car::recode(as.numeric(wvs_full$cp11d039),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_open10 <-car::recode(as.numeric(wvs_full$cp11d049),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#Openness in 2012
wvs_full$y12_open1 <-wvs_full$cp12e024
wvs_full$y12_open2 <-wvs_full$cp12e034 
wvs_full$y12_open3 <-wvs_full$cp12e044
wvs_full$y12_open4 <-wvs_full$cp12e054
wvs_full$y12_open5 <-wvs_full$cp12e059
wvs_full$y12_open6 <-wvs_full$cp12e064
wvs_full$y12_open7 <-wvs_full$cp12e069
#reversed coded items
wvs_full$y12_rec_open8 <-car::recode(as.numeric(wvs_full$cp12e029),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_open9 <-car::recode(as.numeric(wvs_full$cp12e039),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_open10 <-car::recode(as.numeric(wvs_full$cp12e049),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 11 and 12
wvs_full$open1 <- with(wvs_full,rowMeans(data.frame(y12_open1,y11_open1),na.rm=T))
wvs_full$open2 <- with(wvs_full,rowMeans(data.frame(y12_open2,y11_open2),na.rm=T))
wvs_full$open3 <- with(wvs_full,rowMeans(data.frame(y12_open3,y11_open3),na.rm=T))
wvs_full$open4 <- with(wvs_full,rowMeans(data.frame(y12_open4,y11_open4),na.rm=T))
wvs_full$open5 <- with(wvs_full,rowMeans(data.frame(y12_open5,y11_open5),na.rm=T))
wvs_full$open6 <- with(wvs_full,rowMeans(data.frame(y12_open6,y11_open6),na.rm=T))
wvs_full$open7 <- with(wvs_full,rowMeans(data.frame(y12_open7,y11_open7),na.rm=T))
wvs_full$open8 <- with(wvs_full,rowMeans(data.frame(y12_rec_open8,y11_rec_open8),na.rm=T))
wvs_full$open9 <- with(wvs_full,rowMeans(data.frame(y12_rec_open9,y11_rec_open9),na.rm=T))
wvs_full$open10 <- with(wvs_full,rowMeans(data.frame(y12_rec_open10,y11_rec_open10),na.rm=T))

#########################
#Conscientiousness
#########################

#2011
wvs_full$y11_con1 <- wvs_full$cp11d022
wvs_full$y11_con2 <- wvs_full$cp11d032
wvs_full$y11_con3 <- wvs_full$cp11d042
wvs_full$y11_con4 <- wvs_full$cp11d052
wvs_full$y11_con5 <- wvs_full$cp11d062
wvs_full$y11_con6 <- wvs_full$cp11d067
#reverse coded items
wvs_full$y11_rec_con7 <-car::recode(as.numeric(wvs_full$cp11d027),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_con8 <-car::recode(as.numeric(wvs_full$cp11d037),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_con9 <-car::recode(as.numeric(wvs_full$cp11d047),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_con10 <-car::recode(as.numeric(wvs_full$cp11d057),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#2012
wvs_full$y12_con1 <- wvs_full$cp12e022
wvs_full$y12_con2 <- wvs_full$cp12e032
wvs_full$y12_con3 <- wvs_full$cp12e042
wvs_full$y12_con4 <- wvs_full$cp12e052
wvs_full$y12_con5 <- wvs_full$cp12e062
wvs_full$y12_con6 <- wvs_full$cp12e067
#reverse coded items
wvs_full$y12_rec_con7 <-car::recode(as.numeric(wvs_full$cp12e027),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_con8 <-car::recode(as.numeric(wvs_full$cp12e037),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_con9 <-car::recode(as.numeric(wvs_full$cp12e047),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_con10 <-car::recode(as.numeric(wvs_full$cp12e057),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 11 and 12
wvs_full$con1 <- with(wvs_full,rowMeans(data.frame(y12_con1,y11_con1),na.rm=T))
wvs_full$con2 <- with(wvs_full,rowMeans(data.frame(y12_con2,y11_con2),na.rm=T))
wvs_full$con3 <- with(wvs_full,rowMeans(data.frame(y12_con3,y11_con3),na.rm=T))
wvs_full$con4 <- with(wvs_full,rowMeans(data.frame(y12_con4,y11_con4),na.rm=T))
wvs_full$con5 <- with(wvs_full,rowMeans(data.frame(y12_con5,y11_con5),na.rm=T))
wvs_full$con6 <- with(wvs_full,rowMeans(data.frame(y12_con6,y11_con6),na.rm=T))
wvs_full$con7 <- with(wvs_full,rowMeans(data.frame(y12_rec_con7,y11_rec_con7),na.rm=T))
wvs_full$con8 <- with(wvs_full,rowMeans(data.frame(y12_rec_con8,y11_rec_con8),na.rm=T))
wvs_full$con9 <- with(wvs_full,rowMeans(data.frame(y12_rec_con9,y11_rec_con9),na.rm=T))
wvs_full$con10 <- with(wvs_full,rowMeans(data.frame(y12_rec_con10,y11_rec_con10),na.rm=T))

######################
#Extraversion
######################

#*2011
wvs_full$y11_ext1 <- wvs_full$cp11d020
wvs_full$y11_ext2 <- wvs_full$cp11d030
wvs_full$y11_ext3 <- wvs_full$cp11d040
wvs_full$y11_ext4 <- wvs_full$cp11d050
wvs_full$y11_ext5 <- wvs_full$cp11d060
#reversed coded items
wvs_full$y11_rec_ext6 <-car::recode(as.numeric(wvs_full$cp11d025),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_ext7 <-car::recode(as.numeric(wvs_full$cp11d035),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_ext8 <-car::recode(as.numeric(wvs_full$cp11d045),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_ext9 <-car::recode(as.numeric(wvs_full$cp11d055),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_ext10 <-car::recode(as.numeric(wvs_full$cp11d065),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#*2012
wvs_full$y12_ext1 <- wvs_full$cp12e020
wvs_full$y12_ext2 <- wvs_full$cp12e030
wvs_full$y12_ext3 <- wvs_full$cp12e040
wvs_full$y12_ext4 <- wvs_full$cp12e050
wvs_full$y12_ext5 <- wvs_full$cp12e060
#reversed coded items
wvs_full$y12_rec_ext6 <-car::recode(as.numeric(wvs_full$cp12e025),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_ext7 <-car::recode(as.numeric(wvs_full$cp12e035),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_ext8 <-car::recode(as.numeric(wvs_full$cp12e045),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_ext9 <-car::recode(as.numeric(wvs_full$cp12e055),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_ext10 <-car::recode(as.numeric(wvs_full$cp12e065),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 2011 & 2012
wvs_full$ext1 <- with(wvs_full,rowMeans(data.frame(y12_ext1,y11_ext1),na.rm=T))
wvs_full$ext2 <- with(wvs_full,rowMeans(data.frame(y12_ext2,y11_ext2),na.rm=T))
wvs_full$ext3 <- with(wvs_full,rowMeans(data.frame(y12_ext3,y11_ext3),na.rm=T))
wvs_full$ext4 <- with(wvs_full,rowMeans(data.frame(y12_ext4,y11_ext4),na.rm=T))
wvs_full$ext5 <- with(wvs_full,rowMeans(data.frame(y12_ext5,y11_ext5),na.rm=T))
wvs_full$ext6 <- with(wvs_full,rowMeans(data.frame(y12_rec_ext6,y11_rec_ext6),na.rm=T))
wvs_full$ext7 <- with(wvs_full,rowMeans(data.frame(y12_rec_ext7,y11_rec_ext7),na.rm=T))
wvs_full$ext8 <- with(wvs_full,rowMeans(data.frame(y12_rec_ext8,y11_rec_ext8),na.rm=T))
wvs_full$ext9 <- with(wvs_full,rowMeans(data.frame(y12_rec_ext9,y11_rec_ext9),na.rm=T))
wvs_full$ext10 <- with(wvs_full,rowMeans(data.frame(y12_rec_ext10,y11_rec_ext10),na.rm=T))

#########################
#Agreeableness
#########################

#2011
wvs_full$y11_agre1 <- wvs_full$cp11d026
wvs_full$y11_agre2 <- wvs_full$cp11d036
wvs_full$y11_agre3 <- wvs_full$cp11d046
wvs_full$y11_agre4 <- wvs_full$cp11d056
wvs_full$y11_agre5 <- wvs_full$cp11d061
wvs_full$y11_agre6 <- wvs_full$cp11d066
#reversed coded items
wvs_full$y11_rec_agre7 <-car::recode(as.numeric(wvs_full$cp11d021),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_agre8 <-car::recode(as.numeric(wvs_full$cp11d031),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_agre9 <-car::recode(as.numeric(wvs_full$cp11d041),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_agre10 <-car::recode(as.numeric(wvs_full$cp11d051),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#2012
wvs_full$y12_agre1 <- wvs_full$cp12e026
wvs_full$y12_agre2 <- wvs_full$cp12e036
wvs_full$y12_agre3 <- wvs_full$cp12e046
wvs_full$y12_agre4 <- wvs_full$cp12e056
wvs_full$y12_agre5 <- wvs_full$cp12e061
wvs_full$y12_agre6 <- wvs_full$cp12e066
#reversed coded items
wvs_full$y12_rec_agre7 <-car::recode(as.numeric(wvs_full$cp12e021),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_agre8 <-car::recode(as.numeric(wvs_full$cp12e031),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_agre9 <-car::recode(as.numeric(wvs_full$cp12e041),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_agre10 <-car::recode(as.numeric(wvs_full$cp12e051),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 2011 and 2012
wvs_full$agre1 <- with(wvs_full,rowMeans(data.frame(y12_agre1,y11_agre1),na.rm=T))
wvs_full$agre2 <- with(wvs_full,rowMeans(data.frame(y12_agre2,y11_agre2),na.rm=T))
wvs_full$agre3 <- with(wvs_full,rowMeans(data.frame(y12_agre3,y11_agre3),na.rm=T))
wvs_full$agre4 <- with(wvs_full,rowMeans(data.frame(y12_agre4,y11_agre4),na.rm=T))
wvs_full$agre5 <- with(wvs_full,rowMeans(data.frame(y12_agre5,y11_agre5),na.rm=T))
wvs_full$agre6 <- with(wvs_full,rowMeans(data.frame(y12_agre6,y11_agre6),na.rm=T))
wvs_full$agre7 <- with(wvs_full,rowMeans(data.frame(y12_rec_agre7,y11_rec_agre7),na.rm=T))
wvs_full$agre8 <- with(wvs_full,rowMeans(data.frame(y12_rec_agre8,y11_rec_agre8),na.rm=T))
wvs_full$agre9 <- with(wvs_full,rowMeans(data.frame(y12_rec_agre9,y11_rec_agre9),na.rm=T))
wvs_full$agre10 <- with(wvs_full,rowMeans(data.frame(y12_rec_agre10,y11_rec_agre10),na.rm=T))

##############
#*neuroticism
##############

#2011
wvs_full$y11_neu1 <- wvs_full$cp11d023
wvs_full$y11_neu2 <- wvs_full$cp11d033
wvs_full$y11_neu3 <- wvs_full$cp11d043
wvs_full$y11_neu4 <- wvs_full$cp11d048
wvs_full$y11_neu5 <- wvs_full$cp11d053
wvs_full$y11_neu6 <- wvs_full$cp11d058
wvs_full$y11_neu7 <- wvs_full$cp11d063
wvs_full$y11_neu8 <- wvs_full$cp11d068
#reversed coded items
wvs_full$y11_rec_neu9 <-car::recode(as.numeric(wvs_full$cp11d028),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y11_rec_neu10 <-car::recode(as.numeric(wvs_full$cp11d038),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#2012
wvs_full$y12_neu1 <- wvs_full$cp12e023
wvs_full$y12_neu2 <- wvs_full$cp12e033
wvs_full$y12_neu3 <- wvs_full$cp12e043
wvs_full$y12_neu4 <- wvs_full$cp12e048
wvs_full$y12_neu5 <- wvs_full$cp12e053
wvs_full$y12_neu6 <- wvs_full$cp12e058
wvs_full$y12_neu7 <- wvs_full$cp12e063
wvs_full$y12_neu8 <- wvs_full$cp12e068
#reversed coded items
wvs_full$y12_rec_neu9 <-car::recode(as.numeric(wvs_full$cp12e028),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
wvs_full$y12_rec_neu10 <-car::recode(as.numeric(wvs_full$cp12e038),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 2011 and 2012
wvs_full$neu1 <- with(wvs_full,rowMeans(data.frame(y12_neu1,y11_neu1),na.rm=T))
wvs_full$neu2 <- with(wvs_full,rowMeans(data.frame(y12_neu2,y11_neu2),na.rm=T))
wvs_full$neu3 <- with(wvs_full,rowMeans(data.frame(y12_neu3,y11_neu3),na.rm=T))
wvs_full$neu4 <- with(wvs_full,rowMeans(data.frame(y12_neu4,y11_neu4),na.rm=T))
wvs_full$neu5 <- with(wvs_full,rowMeans(data.frame(y12_neu5,y11_neu5),na.rm=T))
wvs_full$neu6 <- with(wvs_full,rowMeans(data.frame(y12_neu6,y11_neu6),na.rm=T))
wvs_full$neu7 <- with(wvs_full,rowMeans(data.frame(y12_neu7,y11_neu7),na.rm=T))
wvs_full$neu8 <- with(wvs_full,rowMeans(data.frame(y12_neu8,y11_neu8),na.rm=T))
wvs_full$neu9 <- with(wvs_full,rowMeans(data.frame(y12_rec_neu9,y11_rec_neu9),na.rm=T))
wvs_full$neu10 <- with(wvs_full,rowMeans(data.frame(y12_rec_neu10,y11_rec_neu10),na.rm=T))

########################
#
#
#Create scales of the IPIP FFM 50 item per trait
#
#
########################


########################################
#
#create IPIP scales: 50 items
#
########################################

#create Openness scale
wvs_full$IPIP_open<-(rowMeans(data.frame(wvs_full$open1, wvs_full$open2, wvs_full$open3, wvs_full$open4, wvs_full$open5, wvs_full$open6, wvs_full$open7, wvs_full$open8, wvs_full$open9, wvs_full$open10), na.rm=T))

#create Conscientiousness scale
wvs_full$IPIP_con<-(rowMeans(data.frame(wvs_full$con1, wvs_full$con2, wvs_full$con3, wvs_full$con4, wvs_full$con5, wvs_full$con6, wvs_full$con7, wvs_full$con8, wvs_full$con9, wvs_full$con10), na.rm=T))

#create Extraversion scale
wvs_full$IPIP_ext<-(rowMeans(data.frame(wvs_full$ext1, wvs_full$ext2, wvs_full$ext3, wvs_full$ext4, wvs_full$ext5, wvs_full$ext6, wvs_full$ext7, wvs_full$ext8, wvs_full$ext9, wvs_full$ext10), na.rm=T))

#create Agreeableness scale
wvs_full$IPIP_agre<-(rowMeans(data.frame(wvs_full$agre1, wvs_full$agre2, wvs_full$agre3, wvs_full$agre4, wvs_full$agre5, wvs_full$ext6, wvs_full$agre7, wvs_full$agre8, wvs_full$agre9, wvs_full$agre10), na.rm=T))

#create Neuroticism scale
wvs_full$IPIP_neu<-(rowMeans(data.frame(wvs_full$neu1, wvs_full$neu2, wvs_full$neu3, wvs_full$neu4, wvs_full$neu5, wvs_full$neu6, wvs_full$neu7, wvs_full$neu8, wvs_full$neu9, wvs_full$neu10), na.rm=T))

########################
#
#
#Create scales for the Mini-IPIP: 4 item per trait
#
#
########################

#create Openness scale
wvs_full$Mini_IPIP_open<-(rowMeans(data.frame(wvs_full$open2, wvs_full$open8, wvs_full$open9, wvs_full$open10), na.rm=T))

#create Conscientiousness scale
wvs_full$Mini_IPIP_con<-(rowMeans(data.frame(wvs_full$con3, wvs_full$con4, wvs_full$con8, wvs_full$con9), na.rm=T))

#create Extraversion scale
wvs_full$Mini_IPIP_ext<-(rowMeans(data.frame(wvs_full$ext1, wvs_full$ext4, wvs_full$ext6, wvs_full$ext7), na.rm=T))

#create Agreeableness scale
wvs_full$Mini_IPIP_agre<-(rowMeans(data.frame(wvs_full$agre2, wvs_full$agre5, wvs_full$agre9, wvs_full$agre10), na.rm=T))

#create Neuroticism scale
wvs_full$Mini_IPIP_neu<-(rowMeans(data.frame(wvs_full$neu4, wvs_full$neu6, wvs_full$neu9, wvs_full$neu10), na.rm=T))

#*******************
#*gender
#*******************
table(wvs_full$geslacht)
#gender: 0=male; 1=female
wvs_full$gender <-car::recode(as.numeric(wvs_full$geslacht),"1=0; 2=1", as.numeric=T)

#*******************
#*education
#********************
wvs_full$education<-wvs_full$oplcat

#*******************
#*Income in categories with 7==missing income
#********************

wvs_full$income_categories[wvs_full$brutohh_f <=1500]=1
wvs_full$income_categories[wvs_full$brutohh_f >1500 & wvs_full$brutohh_f <=2500]=2
wvs_full$income_categories[wvs_full$brutohh_f >2500 & wvs_full$brutohh_f <=3500]=3
wvs_full$income_categories[wvs_full$brutohh_f >3500 & wvs_full$brutohh_f <=4500]=4
wvs_full$income_categories[wvs_full$brutohh_f >4500 & wvs_full$brutohh_f <=5500]=5
wvs_full$income_categories[wvs_full$brutohh_f >5500 & wvs_full$brutohh_f <=1000000]=6
wvs_full$income_categories[is.na(wvs_full$income_categories)]=7

#*******************
#*age
#*******************
#age ranging from 0 - 1
wvs_full$age <- wvs_full$leeftijd
wvs_full$age[wvs_full$age <=17]=NA
table(wvs_full$age)

#wvs_full$age <- ((wvs_full$age-18)/71)

#*******************
#*age^2
#*******************
wvs_full$age2 <-(wvs_full$age*wvs_full$age)

#education dummy's
wvs_full$edu2<-factor(ifelse(as.numeric(wvs_full$education)==2, 1,0))
wvs_full$edu3<-factor(ifelse(as.numeric(wvs_full$education)==3, 1,0))
wvs_full$edu4<-factor(ifelse(as.numeric(wvs_full$education)==4, 1,0))
wvs_full$edu5<-factor(ifelse(as.numeric(wvs_full$education)==5, 1,0))
wvs_full$edu6<-factor(ifelse(as.numeric(wvs_full$education)==6, 1,0))

#income dummy's
wvs_full$income2 <- factor(ifelse(as.numeric(wvs_full$income_categories)==2, 1,0))
wvs_full$income3 <- factor(ifelse(as.numeric(wvs_full$income_categories)==3, 1,0))
wvs_full$income4 <- factor(ifelse(as.numeric(wvs_full$income_categories)==4, 1,0))
wvs_full$income5 <- factor(ifelse(as.numeric(wvs_full$income_categories)==5, 1,0))
wvs_full$income6 <- factor(ifelse(as.numeric(wvs_full$income_categories)==6, 1,0))
wvs_full$income7 <- factor(ifelse(as.numeric(wvs_full$income_categories)==7, 1,0))

#subset data
wvs_sub <- na.omit(select(wvs_full, starts_with("BFI"), starts_with("open"), starts_with("con"), starts_with("ext"), starts_with("agre"),starts_with("neu")  ,306:331, starts_with("wvs"),wvs_lrplacement,purity,gay1,gay2,abortion,euthanasia,immigration,immigration1,redistribution1,govrespon,redistribution2,wvs_moral,wvs_economicattitudes,wvs_lrplacement,nomem_encr))
colnames(wvs_sub)
wvs_sub[,c(1:5,66:75)]<- mapply(FUN = zero1,wvs_sub[,c(1:5,66:75)])
save(wvs_sub, file="wvs_sub.RData")

